<?php

namespace App\Http\Requests;

use App\Models\MedicalRecord;
use App\Models\Patient;
use Illuminate\Database\Query\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\ValidationException;

class MarriageHistoryStoreRequest extends FormRequest
{
    /**
     * @var \App\Project\FieldTranslation\FieldTranslation
     */
    protected $field;

    /**
     * Prepare the data for validation.
     * @throws \Illuminate\Validation\ValidationException
     */
    protected function prepareForValidation()
    {
        $record_id = $this->input('record_id');
        $validator = \Validator::make(['record_id' => $record_id], [
            'record_id' => [
                'required', 'integer', 'min:1',
                Rule::exists('st_medical_records', 'id')->where(function (Builder $query) {
                    $query->whereNull('deleted_at');
                    $user = \Auth::user();
                    if (!$user->isDoctorsAdmin() && !$user->isAdmin()&& !$user->isPatient()) {
                        $query->where(['doctor_id' => $user->id]);
                    }
                }),
            ],
        ], [
            'record_id.required' => '缺少病历ID',
            'record_id.integer'  => '病历ID 必须是整数。',
            'record_id.min'      => '病历ID 必须大于等于 :min。',
            'record_id.exists'   => '病历 不存在或无修改该病历的权限。',
        ]);

        if ($validator->fails()) {
            $this->failedValidation($validator);
        }

        //  检测患者是否为男性，是男性时，中断请求
        //        $patient_id = (new MedicalRecord())->getWithCache($record_id)->patient_id;
        //        $sex = (new Patient())->getWithCache($patient_id)->sex;
        //
        //        if (0 == $sex['key']) {
        //            throw new ValidationException($validator, $this->response(
        //                ['sex' => '男性患者 不能添加 月经婚育史项目']
        //            ));
        //        }

        $this->field = app('field')->get('marriage');
    }

    /**
     * Determine if the user is authorized to make this request.
     * @return bool
     */
    public function authorize()
    {
        return \Auth::check();
    }

    /**
     * Get the validation rules that apply to the request.
     * @return array
     */
    public function rules()
    {
        return [
            'marriage.time'                   => [
                'nullable',
                'date',
                'before_or_equal:now',
            ],
            'marriage.son'                    => 'nullable|integer|min:0|max:99',
            'marriage.daughter'               => 'nullable|integer|min:0|max:99',
            'macrosomia'                      => 'nullable|boolean',
            'menstruation.menarche'           => 'nullable|integer|min:0|max:99',
            'menstruation.period.days'        => 'nullable|integer|min:0|max:999',
            'menstruation.period.law'         => 'boolean',
            'menstruation.cycle.days'         => 'nullable|integer|min:0|max:999',
            'menstruation.cycle.law'          => 'boolean',
            'menstruation.last_time.date'     => 'nullable|date|before_or_equal:now',
            'menstruation.last_time.continue' => 'nullable|integer|min:0|max:31',
            'menstruation.menstrual_volume'   => 'in:' . implode(',',
                    array_keys($this->field['menstruation']['menstrual_volume'])),
            'menopause.age'                   => 'nullable|integer|min:0|max:99',
            'remark'                          => 'nullable|string|max:255',
        ];
    }

    public function messages()
    {
        return [
            'marriage.time.date'            => '日期格式不正确。',
            'marriage.time.before_or_equal' => '结婚时间 必须要早于当前时间。',
            'marriage.son.integer'          => '生育男孩数量 必须是一个数字。',
            'marriage.daughter.integer'     => '生育女孩数量 必须是一个数字。',
            'marriage.son.min'              => '生育男孩数量 必须大于等于 :min。',
            'marriage.daughter.min'         => '生育女孩数量 必须大于等于 :min。',

            'macrosomia' => '输入值的类型错误。',

            'menstruation.menarche.integer'               => '月经初潮年龄 必须是一个数字。',
            'menstruation.menarche.min'                   => '月经初潮年龄 必须大于等于 :min。',
            'menstruation.period.days.integer'            => '经期时间 必须是一个数字。',
            'menstruation.period.days.min'                => '经期时间 必须大于等于 :min。',
            'menstruation.period.law.boolean'             => '输入值的类型必须是布尔值。',
            'menstruation.cycle.days.integer'             => '月经周期 必须是一个数字。',
            'menstruation.cycle.days.min'                 => '月经周期 必须大于等于 :min。',
            'menstruation.cycle.law.boolean'              => '输入值的类型必须是布尔值。',
            'menstruation.menstrual_volume.in'            => '月经量 取值范围错误',
            'menstruation.last_time.date.date'            => '末次月经 必须为时间类型',
            'menstruation.last_time.date.before_or_equal' => '末次月经 时间要早于现在时间',
            'menstruation.last_time.continue.integer'     => '末次月经 时间必须为整数',
            'menstruation.last_time.continue.min'         => '末次月经 时间必须大于0',
            'menstruation.last_time.continue.max'         => '末次月经不能大于31天',

            'menopause.age.integer' => '停经年龄 必须是一个数字。',
            'menopause.age.min'     => '停经年龄 必须大于等于 :min。',

            'remark.max' => '备注最大 :max 个字符',
        ];
    }
}
